//
//  HistoryOrderViewController.m
//  DaiShengInvest
//
//  Created by 张睿 on 2017/7/4.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "HistoryOrderViewController.h"
#import "HistoryCell.h"
#import "OrderDetailsViewController.h"
#import "HistoryListModel.h"

@interface HistoryOrderViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UIImageView * backImageView;

@property (nonatomic, strong)UITableView * tableView;

@property (nonatomic, strong)NSArray<HistoryListModel *>* historyListArray;

@end

@implementation HistoryOrderViewController


- (void)viewWillAppear:(BOOL)animated{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}


- (void)viewDidAppear:(BOOL)animated{
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

- (void)setHistoryListArray:(NSArray<HistoryListModel *> *)historyListArray{
    
    _historyListArray = historyListArray;
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [HistoryListModel historyListWithSucess:^(NSArray<HistoryListModel *> *array) {
        
        self.historyListArray = array;

//        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } error:^{
        NSLog(@"historyList error");
    }];
    
    [self setupNavVC];
    [self setupUI];
    
}



- (void)setupNavVC{
    
    self.title = @"历史订单";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0 * SCREENH6, 0 * SCREENH6, 22 * SCREENH6, 25 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    
    [backButton addTarget:self action:@selector(didBackButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)setupUI{
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backImageView = backImage;
    
    [self.view addSubview:self.backImageView];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];

    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = NO;
    
    [self.tableView registerClass:[HistoryCell class] forCellReuseIdentifier:@"addCell"];
    
    [self.view addSubview:self.tableView];
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.historyListArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addCell" forIndexPath:indexPath];
    
//    cell.dateLabel.text = @"2017.08.01";
//    cell.timeLabel.text = @"17:59:59";
//    cell.operationLabel.text = @"看空";
//    cell.wayLabel.text = @"手动平仓";
//    cell.getMoneyCountLabel.text = @"+$109.79";
//    cell.getMoneyLabel.text = @"收益";
    HistoryListModel * model = self.historyListArray[indexPath.row];
    
    cell.model = model;
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 83 * SCREENH6;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.hidesBottomBarWhenPushed = YES;
    
    HistoryCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    OrderDetailsViewController * orderDetailsVC = [OrderDetailsViewController new];
    
    orderDetailsVC.operationString = cell.operationLabel.text;
    orderDetailsVC.getMoneyCountLabel = cell.getMoneyCountLabel.text;
    orderDetailsVC.nameString = cell.wayLabel.text;
    orderDetailsVC.volumeLabel = cell.volumeLabel;
    orderDetailsVC.initialMarginLabel = cell.initialMarginLabel;
    orderDetailsVC.commissionLabel = cell.commissionLabel;
    orderDetailsVC.swapLabel = cell.swapLabel;
    orderDetailsVC.openPriceLabel = cell.openPriceLabel;
    orderDetailsVC.closePriceLabel = cell.closePriceLabel;
    orderDetailsVC.openTimeLabel = cell.openTimeLabel;
    orderDetailsVC.positionIdLabel = cell.positionIdLabel;
    orderDetailsVC.color = cell.colorString;
    orderDetailsVC.closeTime = cell.closeTime;
    
    [self.navigationController pushViewController:orderDetailsVC animated:YES];
}


- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
}





- (void)didBackButton{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
